package cRedis

import (
	"gitee.com/csingo/cComponents"
	"gitee.com/csingo/cLog"
)

type RedisComponent struct{}

func (i *RedisComponent) Inject(instance any) bool {
	return false
}

func (i *RedisComponent) InjectConf(config cComponents.ConfigInterface) bool {
	if config.ConfigName() == RedisConfigName {
		redis_config = config.(*RedisConf)
		return true
	}

	return false
}

func (i *RedisComponent) Load() {
	for name, config := range redis_config.Clients {
		connector := NewGoRedisConnector(name, config)
		err := connector.Connect()
		if err != nil {
			cLog.WithContext(nil, map[string]any{
				"source": "cRedis.RedisComponent.Load",
				"name":   name,
				"config": config,
				"err":    err.Error(),
			}).Fatal("redis连接异常")
		}
		if container.Is(connector) {
			container.Save(connector)
		}
	}
}

func (i *RedisComponent) Listen() []*cComponents.ConfigListener {
	return []*cComponents.ConfigListener{
		{
			ConfigName: RedisConfigName,
			Listeners: []cComponents.ListenHandler{
				cComponents.DefaultListenHandler,
			},
		},
	}
}

var Component = &RedisComponent{}
